/**
 * Created by yk on 2017/9/22.
 */
var fs = require("fs");

function format(filePath, keywords) {

    var content = fs.readFileSync(filePath, "utf-8");

    var arr = content.split("\n");

    var count = 0;

    for (var i = 0; i < arr.length; i++) {
        var value = arr[i];
        var temp;
        temp = value.replace(/(\d+)px/g, function() {
            console.log(arguments);
            if (arguments[3].indexOf("@media") != -1 || arguments[3].indexOf("max") != -1 || arguments[3].indexOf("min") != -1) {

            } else if (Number(arguments[1]) > 1) {
                count++;
                return Number(arguments[1]) / 50 + "rem";
            }

            return arguments[0];
        });

        if (temp != arr[i]) {
                arr.splice(i, 1, temp);
        }
    }

    var result = arr.join("\n");

    var formatFilePath = filePath.replace(".", keywords + ".");

    fs.writeFile(formatFilePath, result, "utf-8", function(err) {
        if (err) console.log(err);
        console.log("change lines number: " + count);
    });
}

format("index.less", "_format");
